"use strict";
define(["jquery", "template"], function ($, template) {
    (function ($) {
        var ExcelTable = function () {
            function ExcelTable(el, options) {
                this.id = 'excelTable' + Math.round(Math.random() * 1000000000); //实例唯一ID名
                this.defalut = {
                    labelWidth: "110px", //label宽
                    wrap: false,
                    data: [] //数据
                };
                this._set = $.extend(this._defaluts, options);
                this.settings = $.extend(true, this.defalut, options, {
                    id: this.id
                } || []);
                this.el = el;
                this.init();
            };
            ExcelTable.prototype = {
                init: function init() {
                    var option = this.settings;
                    var el = this.el; //组件实例容器
                    this.initHtml(option);
                    $(document).ready(function () {
                        el.trigger('load.bs', option);
                    });
                },

                htmlEscape: function htmlEscape(text) {
                    if (typeof text === "function") {
                        return text().replace(/[<>"&]/g, function (match, pos, originalText) {
                            switch (match) {
                                case "<":
                                    return "&lt;";
                                case ">":
                                    return "&gt;";
                                case "&":
                                    return "&amp;";
                                case "\"":
                                    return "&quot;";
                            }
                        });
                    } else {
                        return text.replace(/[<>"&]/g, function (match, pos, originalText) {
                            switch (match) {
                                case "<":
                                    return "&lt;";
                                case ">":
                                    return "&gt;";
                                case "&":
                                    return "&amp;";
                                case "\"":
                                    return "&quot;";
                            }
                        });
                    }
                },
                initHtml: function initHtml(option) {
                    var that = this;
                    var tpl = '<div class="excel-table">\n                {{each list as item}}\n                <div class="table-item {{if item.col}}item-{{item.col}}{{/if}} {{if item.row}}item-row-{{item.row}}{{/if}}">\n                    <div class="table-label {{if wrap}}wrap{{/if}}" title="{{item.name}}" style="width:' + option.labelWidth + ';min-width:' + option.labelWidth + '" title="{{item.name}}">{{#item.name}}</div>\n                    <div class="table-content {{if wrap}}wrap{{/if}} {{if item.wrap}}itemwrap{{/if}}" style="width:calc(100% - ' + option.labelWidth + ')" title="{{item.value}}">{{#item.value}}</div>\n                </div>\n                {{/each}}\n                </div>';
                    var tableHTML = template.compile(tpl);
                    this.el.html(tableHTML({
                        "wrap": option.wrap,
                        "list": option.data
                    }));
                },
                //监听组件行为
                watchAction: function watchAction() {
                    var el = this.el; //组件实例容器
                    var option = this.settings;
                    var that = this;
                    //插件加载完成
                    el.on('load.bs', function (e, current) {});
                },
                //注册组件事件
                initEvens: function initEvens() {
                    var el = this.el; //组件实例容器
                    var that = this;
                    var option = this.settings;
                    //点击切换下拉菜单
                    el.on('click', '.drap-label,.switchIco', function (e) {});
                    $(window).on('resize', function (e) {});
                }
            };
            return ExcelTable;
        }();

        $.fn.excelTable = function (options) {
            return this.each(function (index, item) {
                var el = $(item);
                var instance = el.data("excelTable");
                //判断实例是否存在
                if (!instance) {
                    instance = new ExcelTable(el, options);

                    //存放实例
                    el.data("excelTable", instance);
                } else {
                    instance = new ExcelTable(el, options);

                    //存放实例
                    el.data("excelTable", instance);
                }

                //外部可以通过$(select).excelTable("init")
                if ($.type(options) === "string") {
                    return instance[options]();
                }
            });
        };

        //插件内部初始化，在目标元素上添加data-excelTable
        $(function () {
            $('[data-excelTable]').excelTable();
        });
    })(jQuery);
})